//
//  NativeViewController.m
//  CJModileAdDemo
//
//  Created by CJ on 2022/3/7.
//

#import "NativeViewController.h"
#import <CJMobileAd/CJMobileAd.h>
#import <BUAdSDK/BUAdSDK.h>

@interface NativeViewController ()<BUNativeExpressAdViewDelegate>

@property (nonatomic,strong) BUNativeExpressAdManager *byte_nativeAd;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray <UIView *>*nativeAdViews;

@end

@implementation NativeViewController

@synthesize customView = _customView;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)dealloc
{
    NSLog(@"事件:%s", __FUNCTION__);
}

- (void)setupUI {
    self.title = @"Native";
    [self.view addSubview:self.customView];
    self.customView.frame = CGRectMake(0, 100, 200, 200);
    self.customView.center = self.view.center;

}

- (void)loadAdData {
    BUAdSlot *slot = [[BUAdSlot alloc] init];
    slot.ID = @"103452829";
    slot.AdType = BUAdSlotAdTypeFeed;
    slot.position = BUAdSlotPositionFeed;
    slot.imgSize = [BUSize sizeBy:BUProposalSize_Feed690_388];
    BUNativeExpressAdManager *nativeManager = [[BUNativeExpressAdManager alloc] initWithSlot:slot adSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width*9/16)];
    nativeManager.delegate = self;
    [nativeManager loadAdDataWithCount:1];
    self.byte_nativeAd = nativeManager;
}

- (void)handleEvent:(NSInteger)eve {
    if (eve == 0) {
        [self loadAdData];
        return;
    }
    
}

- (CustomShowView *)customView {
    if (!_customView) {
        _customView = [[CustomShowView alloc] init];
        __weak NativeViewController *weakSelf = self;
        _customView.event = ^(NSInteger eve) {
            [weakSelf handleEvent:eve];
        };
    }
    return _customView;
}

- (NSMutableArray<UIView *> *)nativeAdViews {
    if (!_nativeAdViews) {
        _nativeAdViews = [NSMutableArray new];
    }
    return _nativeAdViews;
}

#pragma mark delegate datasource
- (void)nativeExpressAdSuccessToLoad:(BUNativeExpressAdManager *)nativeExpressAdManager views:(NSArray<__kindof BUNativeExpressAdView *> *)views {
    BUNativeExpressAdView *view = views.firstObject;
    [view render];
    
    
    CGRect frame = views.firstObject.frame;
    frame.origin.y = 88;
    frame.origin.x = 0;
    views.firstObject.frame = frame;
    [self.view addSubview:views.firstObject];
    NSLog(@"事件:%s", __FUNCTION__);
    NSLog(@"views===%@", views);
    [views enumerateObjectsUsingBlock:^(__kindof BUNativeExpressAdView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        BUNativeExpressAdView *expressView = (BUNativeExpressAdView *)obj;
        expressView.rootViewController = self;
        [expressView render];
    }];
}

- (void)nativeExpressAdFailToLoad:(BUNativeExpressAdManager *)nativeExpressAdManager error:(NSError *_Nullable)error {
    NSLog(@"事件:%s", __FUNCTION__);
}

- (void)nativeExpressAdViewRenderSuccess:(BUNativeExpressAdView *)nativeExpressAdView {
    NSLog(@"事件:%s", __FUNCTION__);
    CGRect frame = nativeExpressAdView.frame;
    frame.origin.y = 88;
    nativeExpressAdView.frame = frame;
    [self.view addSubview:nativeExpressAdView];
}

- (void)nativeExpressAdViewRenderFail:(BUNativeExpressAdView *)nativeExpressAdView error:(NSError *_Nullable)error {
    NSLog(@"事件:%s", __FUNCTION__);
}

- (void)nativeExpressAdViewWillShow:(BUNativeExpressAdView *)nativeExpressAdView {
    NSLog(@"事件:%s", __FUNCTION__);
}

- (void)nativeExpressAdViewDidClick:(BUNativeExpressAdView *)nativeExpressAdView {
    NSLog(@"事件:%s", __FUNCTION__);
}

- (void)nativeExpressAdView:(BUNativeExpressAdView *)nativeExpressAdView dislikeWithReason:(NSArray<BUDislikeWords *> *)filterWords {
    NSLog(@"事件:%s", __FUNCTION__);
    [nativeExpressAdView removeFromSuperview];
}



@end
